package com.atguigu.bean.servlet;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * Date:2020/9/9
 * Author:ybc
 * Description:
 */
public class BaseServlet  {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理编码
        request.setCharacterEncoding("UTF-8");
        //获取唯一标识method的值
        String methodName = request.getParameter("method");
        //判断method的值，根据其不同的值，调用不同的方法
        if(methodName != null && !methodName.equals("")){
            try {
                //获取以methodName为方法名，以HttpServletRequest和HttpServletResponse为参数的方法
                Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
                //忽略方法的访问权限
                method.setAccessible(true);
                //调用所获取的方法，并将真正包含请求信息和响应信息的对象赋值给要调用方法的参数
                method.invoke(this, request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
